package net.jjjshop.shop.service.templ.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import net.jjjshop.common.cache.RegionCache;
import net.jjjshop.common.entity.store.Store;
import net.jjjshop.common.entity.templ.Templ;
import net.jjjshop.common.mapper.store.StoreMapper;
import net.jjjshop.common.mapper.templ.TemplMapper;
import net.jjjshop.common.util.UploadFileUtils;
import net.jjjshop.common.vo.RegionVo;
import net.jjjshop.common.vo.page.PageVo;
import net.jjjshop.config.constant.CommonConstant;
import net.jjjshop.framework.common.service.impl.BaseServiceImpl;
import net.jjjshop.framework.core.pagination.PageInfo;
import net.jjjshop.framework.core.pagination.Paging;
import net.jjjshop.shop.param.CommonPageParam;
import net.jjjshop.shop.param.templ.TemplParam;
import net.jjjshop.shop.service.templ.TemplService;
import net.jjjshop.shop.vo.store.StoreVo;
import net.jjjshop.shop.vo.templ.TemplVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商家门店记录表 服务实现类
 * @author jjjshop
 * @since 2022-07-27
 */
@Slf4j
@Service
public class TemplServiceImpl extends BaseServiceImpl<TemplMapper, Templ> implements TemplService {

    @Autowired
    private RegionCache regionCache;

    @Autowired
    private UploadFileUtils uploadFileUtils;

    public TemplVo getTemplData(Integer templId){
        TemplVo vo = new TemplVo();
        Templ t = this.getById(templId);
        BeanUtils.copyProperties(t, vo);
        return vo;
    }
    /**
     * 商家门店分页查询
     *
     * @param commonPageParam
     * @return
     */
    public Paging<TemplVo> getList(CommonPageParam commonPageParam) {
        Page page = new PageInfo(commonPageParam);
        IPage<Templ> iPage = this.page(page, new LambdaQueryWrapper<Templ>()
                        .comment(CommonConstant.NOT_WITH_App_Id)
                        .eq(Templ::getDeleted, 0)
                        .orderByDesc(Templ::getCreateTime)
                        .orderByDesc(Templ::getUpdateTime));
        IPage<TemplVo> result = iPage.convert(item -> {
            TemplVo vo = new TemplVo();
            BeanUtils.copyProperties(item, vo);
            return vo;
        });
        return new Paging(result);
    }

    /**
     * 获取所有商家门店
     *
     * @param
     * @return
     */
    public List<TemplVo> getAll() {
        List<Templ> list = this.list(new LambdaQueryWrapper<Templ>()
                .comment(CommonConstant.NOT_WITH_App_Id)
                .eq(Templ::getDeleted, 0)
                .orderByDesc(Templ::getCreateTime));
        return list.stream().map(e -> {
            TemplVo vo = new TemplVo();
            BeanUtils.copyProperties(e, vo);
            return vo;
        }).collect(Collectors.toList());
    }

    /**
     * 添加商家门店
     *
     * @param templParam
     * @return
     */
    public Boolean add(TemplParam templParam) {
        Templ store = new Templ();
        BeanUtils.copyProperties(templParam, store);
        return this.save(store);
    }

    /**
     * 编辑商家门店
     *
     * @param templParam
     * @return
     */
    public Boolean edit(TemplParam templParam) {
        Templ store = new Templ();
        BeanUtils.copyProperties(templParam, store);
        return this.updateById(store);
    }

    /**
     * 软删除商家门店
     *
     * @param templId
     * @return
     */
    public Boolean setDelete(Integer templId) {
        return this.update(new LambdaUpdateWrapper<Templ>().eq(Templ::getTemplId, templId).set(Templ::getDeleted, 1));
    }
}
